<?php
	/*
		UserCake
		http://usercake.com
		
		Developed by: Adam Davis
	*/
	require_once("models/config.php");
	require_once("ui.php");

	// this script is not supposed to work
	die();
	
	//Prevent the user visiting the logged in page if he/she is already logged in
	if(isUserLoggedIn()) { header("Location: account.php"); die(); }
?>
<?php
	/* 
		Below is a very simple example of how to process a new user.
		 Some simple validation (ideally more is needed).
		
		The first goal is to check for empty / null data, to reduce workload here
		we let the user class perform it's own internal checks, just in case they are missed.
	*/

//Forms posted
if(!empty($_POST))
{
	$errors = array();
	
	if(empty($_SESSION['6_letters_code'] ) ||
		strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) {
		//Note: the captcha code is compared case insensitively.
		//if you want case sensitive match, update the check above to
		//strcmp()
		$errors[] = "The captcha code does not match";
	}
	
	$email = trim($_POST["email"]);
	$username = trim($_POST["username"]);
	$login = trim($_POST["login"]);
	$password = trim($_POST["password"]);
	$confirm_pass = trim($_POST["passwordc"]);

	//Perform some validation
	//Feel free to edit / change as required
	
	$loginBounds = array(3,25);
	$passwBounds = array(4,50);

	if($username == "")
	{
		$errors[] = lang("ACCOUNT_SPECIFY_USERNAME");
	}
	if(minMaxRangeEx($loginBounds, $login))
	{
		$errors[] = lang("ACCOUNT_USER_CHAR_LIMIT", $loginBounds);
	}
	if(minMaxRangeEx($passwBounds, $password))
	{
		$errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT", $passwBounds);
	}
	else if($password != $confirm_pass)
	{
		$errors[] = lang("ACCOUNT_PASS_MISMATCH");
	}
	if(!isValidEmail($email))
	{
		$errors[] = lang("ACCOUNT_INVALID_EMAIL");
	}
	//End data validation
	if(count($errors) == 0)
	{	
		//Construct a user object
		$user = new User($username,$login,$password,$email);
		
		//Checking this flag tells us whether there were any errors such as possible data duplication occured
		if(!$user->status)
		{
			if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($login));
			if($user->email_taken) 	  $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));		
		}
		else
		{
			//Attempt to add the user to the database, carry out finishing  tasks like emailing the user (if required)
			if(!$user->userCakeAddUser())
			{
				if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
				if($user->sql_failure)  $errors[] = lang("SQL_ERROR");
			}
		}
	}
}
?>
<?php printPageHead(null, null); ?>

	<?php printNavigationBar('login', NAVIGATION_ITEM_REGISTR) ?>		

	<div id="content" class="container_16 clearfix">
       	<h2>TODO: captcha, email activation, form refill</h2>

		<?php
            if(!empty($_POST))
            {
				if(count($errors) > 0) {
            ?>
            <div id="errors">
            	<h2>Registration unsuccessful</h2>
	        	    <?php errorBlock($errors); ?>
            </div>     
            <?php
           	 	} else {
          
		            $message = lang("ACCOUNT_REGISTRATION_COMPLETE_TYPE1");
	        
	    	        if($emailActivation) {
	            	   	 $message = lang("ACCOUNT_REGISTRATION_COMPLETE_TYPE2");
					}
        	?> 

	        <div id="success">
	           <p><?php echo $message ?></p>           
	        </div>
        
        	<?php
				}
             }
			?>

			<div class="grid_5">

	            <div id="regbox">
	                <form name="newUser" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

	                <p>
	                    <label>Name and Surname:</label>
	                    <input type="text" name="username" />
	                </p>
	                
	                <p>
	                    <label>Login:</label>
	                    <input type="text" name="login" />
	                </p>
	                
	                <p>
	                    <label>Password:</label>
	                    <input type="password" name="password" />
	                </p>
	                
	                <p>
	                    <label>Confirm:</label>
	                    <input type="password" name="passwordc" />
	                </p>
	                
	                <p>
	                    <label>Email:</label>
	                    <input type="text" name="email" />
	                </p>
	                
	                <p>
						<img src="models/captcha/captcha_code_file.php?rand=<?php echo rand(); ?>"
						id="captchaimg" >
						<label for="message">Enter the code above here:</label>
						<input id="6_letters_code" name="6_letters_code" type="text">	                
	                </p>
	                
	                <p>
	                    <label>&nbsp;</label>
	                    <input type="submit" value="Register"/>
	                </p>
	                
	                </form>
	            </div>
		</div>
	</div>	
<?php printPageFoot(); ?>
